package cn.edu.scau.cmi.zhengweibo.composite.consistent;

import java.util.Set;

public class ConsistentComposite  extends ConsistentComponent{
	
	public ConsistentComposite(String name) {
		super(name);
	}

	@Override
	public String getName() {
		return this.name;
	}

	@Override
	public void show(String str) {
		System.out.println(str+"["+this.name+"]");
		if(this.child!=null) {
			str+=" ";
			for(ConsistentComponent s : child) {
				s.show(str);
			}
		}
	}
	
	@Override
	public String show1(String str) {
		String newStr=str;
		newStr+= "["+this.name+"]";
		//System.out.println(str+"["+this.name+"]");
		if(this.child!=null) {
			str+=" ";
			for(ConsistentComponent s : child) {
				newStr+="\n"+s.show1(str);
			}
		}
		return newStr;
	}

	@Override
	public void add(ConsistentComponent component) {
		child.add(component);
	}

	@Override
	public void remove(ConsistentComponent component) {
		child.remove(component);
	}

	@Override
	public ConsistentComponent find(String name) {
		for(ConsistentComponent c : child) {
			if(c.getName().equals(name)){
				return c;
			}else if(c.child!=null) {
				for(ConsistentComponent s : c.child) {
					return c.find(name);
				}
			}
		}
		return null;
	}

}
